
package com.wms.ymcc.util;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.sdk.service.dysmsapi20170525.models.*;
import com.aliyun.sdk.service.dysmsapi20170525.*;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import darabonba.core.client.ClientOverrideConfiguration;

//import javax.net.ssl.KeyManager;
//import javax.net.ssl.X509TrustManager;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * 短信发送
 */
public class SendSmsUtil {
    public static void sendSms(String phone,  String code){

        StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                //  阿里云账号AccessKey拥有所有API的访问权限，风险很高,唯一标识
                .accessKeyId("LTAI5tDL3XBaW2u39JtvZpJZ")
                .accessKeySecret("BnDqafHEYE8vrSEJWtTOMdP0bX5Z7m")
                .build());

        AsyncClient client = AsyncClient.builder()
                .region("cn-qingdao") // Region ID
                .credentialsProvider(provider)
                .overrideConfiguration(
                        ClientOverrideConfiguration.create()
                                .setEndpointOverride("dysmsapi.aliyuncs.com")
                )
                .build();

        JsonObject  jsonObject = new JsonObject();
        jsonObject.addProperty("code", code);
        String jsonString = JSONObject.toJSONString(jsonObject);

        SendSmsRequest sendSmsRequest = SendSmsRequest.builder()
                .signName("阿里云短信测试")
                .templateCode("SMS_154950909")
                .phoneNumbers(phone)
                .templateParam(jsonString)
                .build();

        CompletableFuture<SendSmsResponse> response = client.sendSms(sendSmsRequest);
        SendSmsResponse resp = null;
        try {
            resp = response.get();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }
        System.out.println(new Gson().toJson(resp));
        client.close();
    }

}